﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ARR.Entities;
using ARR.UI.Views;
using Telerik.Windows.Controls.Charting;
using System.Windows.Input;
using ARR.UI.Commands;
using ARR.UI.QueriesService;


namespace ARR.UI.ViewModels
{
    public class ChartTestingViewModel : ViewModelBase
    {
        private List<TopSalesCategory> radChartSource;

        public ChartTestingViewModel()
        {
            GenerateCharts();

            //InsertImageinPDF();
        }

        private ICommand addItemsCommand;

        public ICommand AddItemsCommand
        {
            get
            {
                if (addItemsCommand == null)
                {
                    addItemsCommand = new RelayCommand(c => AddItems());
                }
                return addItemsCommand;
            }
        }

        private void AddItems()
        {
            radChartSource.Add(new TopSalesCategory
            {
                Category = "New Category",
                Month = DateTime.Today.AddMonths(1),
                Total = 2020               
            });

            OnPropertyChanged<ChartTestingViewModel>(p => p.RadChartSource);
        }
        
        public List<TopSalesCategory> RadChartSource
        {
            get { return new List<TopSalesCategory>(radChartSource); }
            set { radChartSource = value; }
        }

        private void GenerateCharts()
        {
            QueriesServiceClient queries = new QueriesServiceClient();

            List<TopSalesCategory> topCategories = queries.GetTopSalesCategories();

            radChartSource = topCategories;

            //OnPropertyChanged<ChartTestingViewModel>(p => p.RadChartSource);

            List<TopSalesCategory> aux = new List<TopSalesCategory>();
            DateTime month = DateTime.Today;
            foreach (var item in topCategories)
            {
                aux.Add(new TopSalesCategory { Category = item.Category, Total = item.Total, Month = month });
                aux.Add(new TopSalesCategory { Category = item.Category, Total = item.Total / 2, Month = month.AddMonths(-1) });
                aux.Add(new TopSalesCategory { Category = item.Category, Total = item.Total / 3, Month = month.AddMonths(-2) });
            }


            //PrepareStackedChartPresentation();
            //PrepareDataForStackedChart(aux);
        }

        //private void PrepareStackedChartPresentation()
        //{
        //    //Esto se podria definir en el xaml.
        //    radChartBar.DefaultView.ChartLegend.Header = "Categories";
        //    radChartBar.DefaultView.ChartLegendPosition = Dock.Left;
        //    radChartBar.DefaultView.ChartTitle.Content = "Top 5 Sales Last 3 Months";


        //    AxisX x = new AxisX();
        //    x.IsDateTime = true;
        //    x.DefaultLabelFormat = "MMMM";
        //    x.LabelRotationAngle = 45;
        //    radChartBar.DefaultView.ChartArea.AxisX = x;
        //}

        //private void FillSampleChartData()
        //{
        //    SeriesMapping sm1 = new SeriesMapping();
        //    StackedBarSeriesDefinition d1 = new StackedBarSeriesDefinition();
        //    d1.StackGroupName = "Stack1";
        //    sm1.SeriesDefinition = d1;
        //    sm1.LegendLabel = "Stacked Bar 1";
        //    sm1.CollectionIndex = 0;

        //    ItemMapping im1 = new ItemMapping();
        //    im1.DataPointMember = DataPointMember.YValue;

        //    sm1.ItemMappings.Add(im1);

        //    SeriesMapping sm2 = new SeriesMapping();
        //    StackedBarSeriesDefinition d2 = new StackedBarSeriesDefinition();
        //    d2.StackGroupName = "Stack1";
        //    sm2.SeriesDefinition = d2;
        //    sm2.LegendLabel = "Stacked Bar 2";
        //    sm2.CollectionIndex = 1;

        //    ItemMapping im2 = new ItemMapping();
        //    im2.DataPointMember = DataPointMember.YValue;

        //    sm2.ItemMappings.Add(im2);

        //    SeriesMapping sm3 = new SeriesMapping();
        //    StackedBarSeriesDefinition d3 = new StackedBarSeriesDefinition();
        //    d3.StackGroupName = "Stack1";
        //    sm3.SeriesDefinition = d3;
        //    sm3.LegendLabel = "Stacked Bar 3";
        //    sm3.CollectionIndex = 2;

        //    ItemMapping im3 = new ItemMapping();
        //    im3.DataPointMember = DataPointMember.YValue;

        //    sm3.ItemMappings.Add(im3);

        //    SeriesMapping sm4 = new SeriesMapping();
        //    StackedBarSeriesDefinition d4 = new StackedBarSeriesDefinition();
        //    d4.StackGroupName = "Stack2";
        //    sm4.SeriesDefinition = d4;
        //    sm4.LegendLabel = "Stacked Bar 4";
        //    sm4.CollectionIndex = 3;

        //    ItemMapping im4 = new ItemMapping();
        //    im4.DataPointMember = DataPointMember.YValue;

        //    sm4.ItemMappings.Add(im4);

        //    SeriesMapping sm5 = new SeriesMapping();
        //    StackedBarSeriesDefinition d5 = new StackedBarSeriesDefinition();
        //    d5.StackGroupName = "Stack2";
        //    sm5.SeriesDefinition = d5;
        //    sm5.LegendLabel = "Stacked Bar 5";
        //    sm5.CollectionIndex = 4;

        //    ItemMapping im5 = new ItemMapping();
        //    im5.DataPointMember = DataPointMember.YValue;

        //    sm5.ItemMappings.Add(im5);

        //    radChartBar.SeriesMappings.Add(sm1);
        //    radChartBar.SeriesMappings.Add(sm2);
        //    radChartBar.SeriesMappings.Add(sm3);
        //    radChartBar.SeriesMappings.Add(sm4);
        //    radChartBar.SeriesMappings.Add(sm5);

        //    List<double[]> itemsSource = new List<double[]>();
        //    for (int i = 0; i < 5; i++)
        //    {
        //        itemsSource.Add(SeriesExtensions.GetUserData(5, i));
        //    }

        //    radChartBar.ItemsSource = itemsSource;
        //}

        //private void FillSampleChartData2()
        //{
        //    SeriesMapping sm1 = new SeriesMapping();
        //    StackedBarSeriesDefinition d1 = new StackedBarSeriesDefinition();
        //    d1.StackGroupName = "Stack1";
        //    sm1.SeriesDefinition = d1;
        //    sm1.LegendLabel = "Beverages";
        //    sm1.CollectionIndex = 0;

        //    ItemMapping im1 = new ItemMapping();
        //    im1.DataPointMember = DataPointMember.YValue;


        //    sm1.ItemMappings.Add(im1);

        //    SeriesMapping sm2 = new SeriesMapping();
        //    StackedBarSeriesDefinition d2 = new StackedBarSeriesDefinition();
        //    d2.StackGroupName = "Stack1";
        //    sm2.SeriesDefinition = d2;
        //    sm2.LegendLabel = "Condiments";
        //    sm2.CollectionIndex = 1;

        //    ItemMapping im2 = new ItemMapping();
        //    im2.DataPointMember = DataPointMember.YValue;

        //    sm2.ItemMappings.Add(im2);

        //    SeriesMapping sm3 = new SeriesMapping();
        //    StackedBarSeriesDefinition d3 = new StackedBarSeriesDefinition();
        //    d3.StackGroupName = "Stack1";
        //    sm3.SeriesDefinition = d3;
        //    sm3.LegendLabel = "Snacks";
        //    sm3.CollectionIndex = 2;

        //    ItemMapping im3 = new ItemMapping();
        //    im3.DataPointMember = DataPointMember.YValue;

        //    sm3.ItemMappings.Add(im3);

        //    SeriesMapping sm4 = new SeriesMapping();
        //    StackedBarSeriesDefinition d4 = new StackedBarSeriesDefinition();
        //    d4.StackGroupName = "Stack1";
        //    sm4.SeriesDefinition = d4;
        //    sm4.LegendLabel = "Bottles";
        //    sm4.CollectionIndex = 3;

        //    ItemMapping im4 = new ItemMapping();
        //    im4.DataPointMember = DataPointMember.YValue;

        //    sm4.ItemMappings.Add(im4);

        //    SeriesMapping sm5 = new SeriesMapping();
        //    StackedBarSeriesDefinition d5 = new StackedBarSeriesDefinition();
        //    d5.StackGroupName = "Stack1";
        //    sm5.SeriesDefinition = d5;
        //    sm5.LegendLabel = "Salad";
        //    sm5.CollectionIndex = 4;

        //    ItemMapping im5 = new ItemMapping();
        //    im5.DataPointMember = DataPointMember.YValue;

        //    sm5.ItemMappings.Add(im5);

        //    radChartBar.SeriesMappings.Add(sm1);
        //    radChartBar.SeriesMappings.Add(sm2);
        //    radChartBar.SeriesMappings.Add(sm3);
        //    radChartBar.SeriesMappings.Add(sm4);
        //    radChartBar.SeriesMappings.Add(sm5);

        //    List<double[]> itemsSource = new List<double[]>();
        //    for (int i = 0; i < 5; i++)
        //    {
        //        itemsSource.Add(SeriesExtensions.GetUserData(5, i));
        //    }

        //    radChartBar.ItemsSource = itemsSource;
        //}

        //private void PrepareDataForStackedChart(List<TopSalesCategory> topCategories)
        //{
        //    var categories = (from cat in topCategories
        //                      select cat.Category).Distinct();

        //    List<List<TopSalesCategory>> itemsSource = new List<List<TopSalesCategory>>();
        //    List<TopSalesCategory> list;
        //    int counter = 0;



        //    SeriesMapping serie;
        //    foreach (string categoryName in categories)
        //    {
        //        list = (from item in topCategories
        //                where item.Category == categoryName
        //                select item).ToList();

        //        string groupName = counter < 3 ? "Stack1" : "Stack2";
        //        serie = new SeriesMapping();
        //        serie.SeriesDefinition = new StackedBarSeriesDefinition(groupName);
        //        serie.ItemMappings.Add(new ItemMapping("Total", DataPointMember.YValue));
        //        serie.ItemMappings.Add(new ItemMapping("Month", DataPointMember.XCategory));
        //        serie.LegendLabel = categoryName;
        //        serie.CollectionIndex = counter;

        //        radChartBar.SeriesMappings.Add(serie);

        //        itemsSource.Add(list);

        //        counter++;
        //    }

        //    radChartBar.ItemsSource = itemsSource;
        //}

        private void HelloWorldPdf()
        {
            try
            {
                iTextSharp.text.Document oDoc = new iTextSharp.text.Document();
                PdfWriter.GetInstance(oDoc, new FileStream("HelloWorld.pdf", FileMode.Create));
                oDoc.Open();
                oDoc.Add(new Paragraph("Hello World!"));
                oDoc.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void InsertImageinPDF()
        {

            //Already existing file
            string pdfTemplate = "HelloWorld.pdf";//@"C:\PDFFileSoftware\PdfGenerator_CS\PdfGenerator_CS\PdfGenerator\1_Form SS-4 (2007).pdf";

            //@”c:\Temp\PDF\fw4.pdf”;
            string newFile = @"completed_fw4.pdf";//c:\Temp\PDF\

            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
            AcroFields pdfFormFields = pdfStamper.AcroFields;

            string chartLoc = string.Empty;

            chartLoc = @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg";//@"C:\Temp\PDF\IMG_3746.jpg";//pplLogoSmall.jpg";
            iTextSharp.text.Image chartImg = iTextSharp.text.Image.GetInstance(chartLoc);

            iTextSharp.text.pdf.PdfContentByte underContent;
            iTextSharp.text.Rectangle rect;

            try
            {
                Single X, Y;
                int pageCount = 0;

                rect = pdfReader.GetPageSizeWithRotation(1);
                if (chartImg.Width > rect.Width || chartImg.Height > rect.Height)
                {

                    chartImg.ScaleToFit(rect.Width, rect.Height);

                    X = (rect.Width - chartImg.ScaledWidth) / 2;

                    Y = (rect.Height - chartImg.ScaledHeight) / 2;

                }

                else
                {

                    X = (rect.Width - chartImg.Width) / 2;

                    Y = (rect.Height - chartImg.Height) / 2;

                } chartImg.SetAbsolutePosition(X, Y);

                pageCount = pdfReader.NumberOfPages;
                for (int i = 1; i <= pageCount; i++)
                {

                    underContent = pdfStamper.GetOverContent(i);//.GetUnderContent(i);

                    underContent.AddImage(chartImg);

                }

                pdfStamper.Close();

                pdfReader.Close();

            }
            catch (Exception ex)
            {
                throw ex;

            }

        }                
    }
}
